package com.sz.wallet_demo.config;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.sz.wallet_demo.vo.CurrVo;
import lombok.experimental.UtilityClass;

/**
 * @ClassName CurrContextHolder
 * @Description 当前线程 ThreadLocal 工具类 包含当前用户等信息
 * @Author Ts-SuSu
 * @Date 2023/9/1 13:26
 **/
@UtilityClass
public class CurrContextHolder {

    private final ThreadLocal<CurrVo> THREAD_LOCAL_SYS_CURR = new TransmittableThreadLocal<>();

    /**
     * 设置用户信息
     *
     * @param
     */
    public void setUser(CurrVo currVo) {
        THREAD_LOCAL_SYS_CURR.set(currVo);
    }

    /**
     * 获取当前用户信息
     *
     * @return
     */
    public CurrVo getCurr() {
        return THREAD_LOCAL_SYS_CURR.get();
    }

    /**
     * 获取当前用户信息 且非空 如果为空则抛出未登陆异常
     *
     * @return
     */
    public CurrVo getNotNullCurr() {
        CurrVo currVo = THREAD_LOCAL_SYS_CURR.get();
        if (currVo == null) {
            throw new IllegalStateException("用户未登录!");
        }
        return currVo;
    }

    /**
     * 移除当前用户信息
     */
    public void clear() {
        THREAD_LOCAL_SYS_CURR.remove();
    }


}
